package com.nidu.demo.role.model;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseStatusEntity;
import com.nidu.demo.base.BaseTenantEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.enums.RoleCodeEnum;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.util.Set;

/**
 * 角色领域对象
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class Role extends BaseTenantEntity implements BaseStatusEntity, BaseDeletableEntity {

    private Long id;

    /**
     * 角色名称
     */
    private String roleName;

    /**
     * 角色编码
     */
    private String roleCode;

    /**
     * 角色类型
     */
    private Integer roleType;

    /**
     * 数据权限范围
     */
    private Integer dataScope;

    /**
     * 数据权限范围(指定部门ID数组)
     */
    private Set<Long> dataScopeDeptIds;

    /**
     * 描述
     */
    private String remark;

    /**
     * 排序值
     */
    private Integer sortOrder;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 是否删除
     */
    private Boolean deleted;

    /**
     * 创建角色
     * @param name
     * @param code
     * @param type
     * @param dataScope
     * @param dataScopeDeptIds
     * @param description
     * @param sortOrder
     * @return
     */
    public static Role create(String name, String code, Integer type, Integer dataScope, Set<Long> dataScopeDeptIds, String remark, Integer sortOrder) {
        Role role = new Role();
        role.setRoleName(name)
                .setRoleCode(code)
                .setRoleType(type)
                .setDataScope(dataScope)
                .setDataScopeDeptIds(dataScopeDeptIds)
                .setRemark(remark)
                .setSortOrder(sortOrder);
        return role;
    }

    /**
     * 修改角色
     * @param id
     * @param name
     * @param code
     * @param description
     * @param type
     * @param sortOrder
     * @return
     */
    public static Role update(Long id, String name, String code, Integer type, Integer dataScope, Set<Long> dataScopeDeptIds, String remark, Integer sortOrder) {
        Role role = new Role();
        role.setId(id)
                .setRoleName(name)
                .setRoleCode(code)
                .setRoleType(type)
                .setDataScope(dataScope)
                .setDataScopeDeptIds(dataScopeDeptIds)
                .setRemark(remark)
                .setSortOrder(sortOrder);
        return role;
    }

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.getDeleted())) {
            throw new BizException(ErrorCodeConstants.ROLE_NOT_FOUND.getCode(), ErrorCodeConstants.ROLE_NOT_FOUND.getMessage());
        }
    }

    @Override
    public void isDisable() {
        if (StatusEnum.DISABLE.getValue().equals(this.status)) {
            throw new BizException(ErrorCodeConstants.ROLE_STATUS_DISABLE.getCode(), ErrorCodeConstants.ROLE_STATUS_DISABLE.getMessage());
        }
    }

    public boolean isSuperAdmin(){
        return RoleCodeEnum.isSuperAdmin(this.roleCode);
    }

}